package com.roy.sms.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.roy.sms.service.RoleService;
import com.roy.sms.vo.RoleVO;
import com.roy.sms.vo.UserVO;

@Controller
public class RoleController extends BaseController{

	@Resource
	private RoleService roleService;
	
	@RequestMapping("/role/manager.do")
	public String manager(){
		return "role/roleManager";
	}
	
	@RequestMapping("/role/list.do")
	@ResponseBody
	public List<RoleVO> list(){
		return roleService.list();
	}
	
	@RequestMapping("/role/initEdit.do")
	public String initEdit(Integer id, ModelMap modelMap){
		RoleVO roleVO = null;
		if(null != id){
			roleVO = roleService.loadRole(id);
		} else {
			roleVO = new RoleVO();
		}
		modelMap.put("roleVO", roleVO);
		return "role/editRole";
	}
	
	@RequestMapping("/role/saveRole.do")
	@ResponseBody
	public String saveRole(RoleVO roleVO){
		if(roleVO.getId() == 0){
			roleService.add(roleVO);
		} else {
			roleService.update(roleVO);
		}
		return SUCCESS;
	}
	
	@RequestMapping("/role/deleteRoles.do")
	@ResponseBody
	public String deleteRoles(String ids){
		roleService.deleteRoles(ids);
		return SUCCESS;
	}
	
	@RequestMapping("/role/listRoleUsers.do")
	@ResponseBody
	public List<UserVO> listRoleUser(int roleId){
		return roleService.listRoleUsers(roleId);
	}
	
	@RequestMapping("/role/roleUserManager.do")
	public String roleUserManager(int roleId, ModelMap modelMap){
		RoleVO roleVO = roleService.loadRole(roleId);
		modelMap.put("roleVO", roleVO);
		return "role/roleUserManager";
	}
	
	@RequestMapping("/role/addRoleUser.do")
	@ResponseBody
	public String addRoleUsers(String ids, int roleId){
		roleService.addRoleUsers(ids, roleId);
		return SUCCESS;
	}
	
	@RequestMapping("/role/removeRoleUser.do")
	@ResponseBody
	public String removeRoleUsers(String ids,int roleId){
		roleService.removeRoleUsers(ids, roleId);
		return SUCCESS;
	}
	
	@RequestMapping("/role/roleColumnIds.do")
	@ResponseBody
	public List<Integer> getRoleColumns(int roleId){
		return roleService.getRoleColumnIds(roleId);
	}
	
	@RequestMapping("/role/addRoleColumns.do")
	@ResponseBody
	public String saveRoleColumns(int roleId, String ids){
		roleService.addRoleColumns(roleId, ids);
		return SUCCESS;
	}
}
